package com.ding.easyme.app.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	public static boolean isNull(String str) {
		return str==null||"".equals(str)?true:false;
	}

	public static boolean isContainTag(String str) {//是否包含${TAG}
		int index = str.indexOf("${");
		if(index >= 0) {
			int endindex = str.indexOf("}", index);
			if(endindex >= 0) {
				return true;
			}
		}
		return false;
//		return str.matches(".*[$][{].+[}].*");
	}
	
	
	
	public static String repaceFirstTagStr(String str) {
		return str.replaceFirst("[$][{].+[}]", str);
	}
	
	public static String formatStringNull(String str, String defaultValue) {
		return isNull(str)?defaultValue:str;
	}
	
	public static String formatUrl(String iUrl, String oUrl) {
		if(StringUtil.isNull(oUrl)) return iUrl;
		if(oUrl.startsWith("http://")) return oUrl;
		if(!iUrl.startsWith("http://")) return oUrl;
		if(oUrl.startsWith("../")) {
			int pos = iUrl.lastIndexOf("/");
			if(pos < 0) return oUrl;
			pos = iUrl.lastIndexOf("/", pos - 1);
			if(pos < 0) return oUrl;
			return formatUrl(iUrl.substring(0, pos + 1), oUrl.substring(3));
//			return iUrl.substring(0, pos) + oUrl.substring(2);
		} else if(oUrl.startsWith("/")) {
			int pos = iUrl.indexOf("/", 7);
			if(pos < 0) {
				return iUrl + oUrl;
			} else {
				return iUrl.substring(0, pos) + oUrl;
			}
		} else {
			if(iUrl.endsWith("/")) {
				return iUrl + oUrl;
			} else if(oUrl.startsWith("?")) {
				int pos = iUrl.lastIndexOf("?");
				if(pos < 0) return iUrl + oUrl;
				return iUrl.substring(0, pos) + oUrl;
			}else {
				int pos = iUrl.lastIndexOf("/");
				if(pos < 0) return oUrl;
				return iUrl.substring(0, pos + 1) + oUrl;
			}
		}
	}
	
	public static String byteToStr(byte b) {
		int x = (b & 0xf0) >> 4;
		int y = (b & 0x0f);
		return Character.forDigit(x, 16) + "" + Character.forDigit(y, 16);
	}
	
	public static String byteToStr(byte[] b) {
		if (b==null) return "";
		String str = "";
		for(int i=0; i<b.length; i++) {
			str += byteToStr(b[i]);
		}
		return str;
	}
	
	public static byte strToByte(String str) {
		int x = Integer.valueOf(str.charAt(0) + "", 16);
		int y =Integer.valueOf(str.charAt(1) + "", 16);
		return (byte) ((x & 0x0f) << 4 | (y & 0x0f));
	}
	
	public static String formatChinese(String str) throws UnsupportedEncodingException {
		byte[] b = str.getBytes("GBK");
		return byteToStr(b);
	}
	
	public static String getChineseFromStr(String str) throws UnsupportedEncodingException {
		if(str == null) return "";
		byte[] b = new byte[str.length()/2];
		for(int i =0; i<str.length(); i+=2) {
			b[i/2] = strToByte(str.substring(i, i + 2));
		}
		return new String(b, "GBK");
	}
	
	
	public static void main(String[] args) throws IOException {
//		System.out.println(getFirstTagStr("ab${abc sdf}c"));
//		System.out.println(formatUrl("http://guijj.com/dpgs-7-1/index-3.html", "../dpgs-7-1/index_2.html"));
//		System.out.println(byteToStr((byte) 0x0e));
//		String str = formatChinese("汉亠");
//		System.out.println(str);
//		System.out.println(getChineseFromStr(str));
		
//		System.out.println("sdf<a href=asdf>sdkfksf</a>sdf<div>sdf</div>sdf".replaceAll("[<]a.*[/]a[>]", ""));
//		System.out.println("<A title=\"\" href=\"http://www.meishichina.com/YuanLiao/LianZi/\" target=_blank>莲子</A>30g、".replaceAll("[<]A.*[/]A[>]", ""));
		
//		System.out.println("abcabc".lastIndexOf("abc", 4));
//		System.out.println("sd<imag sdf=>sfdsf</sdf>".replaceAll("[<].*[>]", ""));
//		String str = FileUtil.readStringFromFile(new File("e:/work/Collect/Collect/tpl/sky博客/oTpl_html"));
//		System.out.println(str);
//		System.out.println(str.indexOf("${"));
//		String str = FileUtil.readStringFromFile(new File("D:/work/Collect/temp/de28f68184d01a1d89d245c2"));
////		str = str.replaceAll("\\\\\\\\", "\\\\");
//		String[] temps = str.split("\\\\u");
//		String x = "";
//		for(int i=0; i<temps.length; i++) {
//			if(i == 0) {
//				x = temps[i];
//			} else {
//				x += ((char) Integer.parseInt(temps[i].substring(0, 4), 16)) + temps[i].substring(4);
//			}
//		}
//
//		System.out.println(x);
		
		
//		String s = "http://www.meici.com/product-Miu Miu-pink-woman-shoulder-bags-010502-9389.html";
//		System.out.println(URLEncoder.encode(s));
//		String s = "a<P id=dz>原文地址：<A href=\"http://home.meishichina.com/space-182572-do-blog-id-196261.html\" target=_blank>http://home.meishichina.com/space-182572-do-blog-id-196261.html</A></P>b";
//		System.out.println(s.replaceAll("<P id=dz>原文地址：.*</P>", ""));
//		String s = "<A title=\"\" href=\"http://www.meishichina.com/YuanLiao/LianZi/\" target=_blank>莲子</A>30g、";
//		System.out.println(s.replaceAll("<(A|/A)[^>]*>", ""));
//		String iUrl = "http://www.chinaopre.net/html/2011-06/2710.html";
//		String oUrl = "../../html/2011-06/2710.html";
//		System.out.println(formatUrl(iUrl, oUrl));
		
//		String iUrl = "http://www.chinaopre.net/html/2011-06/2710.html?pn=343";
//		String oUrl = "?pn=14";
//		System.out.println(formatUrl(iUrl, oUrl));
		
//		String s = "<li style=\"background:url(http://img.baidu.com/img/iknow/exp/editor/icon_list_item_1.gif) no-repeat 0px 3px;";
//		System.out.println(s.replaceAll("background:url[^)]*[)]", ""));
		
		
//		String str = HttpRemoteRequest.getMethod(new HttpClient(), "http://www.iq662.com/html/15/20120703-165542.htm");
//		str = new String(str.getBytes("iso-8859-1"), "GBK");
//		String str = "a<strong>推荐栏目：</strong>b\nsdf";
//		System.out.println(str.replaceAll("<strong>推荐栏目：</strong>[\\s\\S]*", ""));
		
//		String str = "a<a href=\"\">sdf</a>b";
//		System.out.println(str.replaceAll("<[Aa].*href.*?/[Aa]>(?#去除所有链接及链接元素本身)", ""));
		
//		String str = "http://piccache.soso.com/namepic/1/u6731/u6731u81eau6e05.png";
//		System.out.println(str.replaceAll("/\\d/", "/3/"));
		
//		byte[] b = "难得清".getBytes("utf-8");
//		String str = "";
//		for(int i=0; i<b.length/2; i++) {
//			str += "\\u";
//			str += TypeConvert.byteToHexStr(b[i*2]);
//			str += TypeConvert.byteToHexStr(b[i*2+1]);
//		}
//		String str = FileUtil.readStringFromFile(new File("E:\\work\\Collect\\2.0\\1000个爱情笑话\\tpl\\tpl_html_article"));
//		System.out.println(str);
		
//		Pattern p=Pattern.compile("<div class=\"top\">(.*?)</div>");
//		Matcher m=p.matcher("a<div class=\"top\">b</div>c<div class=\"top\">d</div>e");
//		if(m.find()) {
//			System.out.println(m.group(1));	
//		} else {
//			System.out.println("false");
//		}
//		
//		if(m.find()) {
//			System.out.println(m.group(1));	
//		} else {
//			System.out.println("false");
//		}
		
//		System.out.println("onclick=\"refreshIdom(this)\" />怀才<font color=red>抱</font>器&nbsp;&bull;&nbsp;智勇<font color=red>兼</font>全</div>".replaceAll("</?font?.*?>", "").replaceAll("&nbsp;", "").replace("&bull;", "·"));
		String str = "    	      	<div class=\"sm\"  onmouseout=\"modifyhidden(this,'sm')\" onmouseover=\"modifydisplay(this,'sm sm_bg')\"><input type=\"submit\" class=\"s_button1\" value=\" 修改名片 \" onclick=\"refreshIdom(this)\" />连中三<font color=red>元</font>&nbsp;&bull;&nbsp;弄<font color=red>璋</font>之喜</div>";
		Pattern p = Pattern.compile("onclick=\"refreshIdom\\(this\\)\" />(.*?)</div>");
		Matcher m=p.matcher(str);
		if(m.find()) {
			System.out.println(m.group(1));
		} else {
			System.out.println(false);
		}
		
	}
}
